<html>
<head><meta charset="utf-8"><title>Iterator over mutable container without unsafe code · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html">Iterator over mutable container without unsafe code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214811835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214811835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214811835">(Oct 28 2020 at 09:30)</a>:</h4>
<p>Hello!<br>
I am trying to implement an iterator over a mutable container without using unsafe code.<br>
For the sake of this example, I tried to re-implement <code>slice::IterMut</code>: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8dbb7ff77d9dfc3dd89c8501d29c561a">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8dbb7ff77d9dfc3dd89c8501d29c561a</a><br>
But I always end up being stopped by the borrow checker because of lifetime constraints.<br>
I think I understand why the borrow checker is complaining, but I cannot seem to find a way to overcome this limitation without going through raw pointer ... And even by doing it, I am not exactly sure how to implement something that is sound.</p>



<a name="214816291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214816291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214816291">(Oct 28 2020 at 10:15)</a>:</h4>
<p>Indeed, I find that that exercise in particular teaches a lot, so good choice <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span> </p>
<p>So, the idea here is that in the signature of <code>next</code>, the input is a <code>&amp;'next mut Iter&lt;'iter&gt;</code>, so "basically", a <code>&amp;'next mut &amp;'iter mut [T]</code>. And the returned type needs to have a lifetime of <code>'iter</code> (this is what allows having the values yielded by multiple <code>.next()</code> calls co-exist and be collected), but the issue is that when "collapsing" borrows / indirection like in this case, the direct mechanism available is "reborrowing", which yields <code>&amp;'next mut [T]</code>, so we end up with the shorter lifetime and fail (for the <em>shared</em> references case, since <code>&amp;_</code> is <code>Copy</code>-able, instead of reborrowing  one can copy the inner reference, and obtain the desired <code>&amp;'iter [T]</code> out of  a <code>&amp;'next &amp;'iter [T]</code>).</p>
<p>In the <em>exclusive</em> (<code>&amp;mut</code>) case, the "equivalent" of "copying" the inner reference is to <em>take</em> it, <em>i.e.</em>, to <em>replace</em> it with a dummy one:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">IterMut</span><span class="o">&lt;'</span><span class="na">iter</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">slice</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">iter</span> <span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">],</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;'</span><span class="na">iter</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Iterator</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">IterMut</span><span class="o">&lt;'</span><span class="na">iter</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">iter</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">next</span><span class="o">&lt;'</span><span class="na">next</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">next</span> <span class="nc">mut</span><span class="w"> </span><span class="n">IterMut</span><span class="o">&lt;'</span><span class="na">iter</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span>-&gt; <span class="nb">Option</span><span class="o">&lt;&amp;'</span><span class="na">iter</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">at_slice</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">next</span> <span class="nc">mut</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">iter</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">slice</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="c1">// Take the slice by putting a temporary dummy in its stead.</span>
<span class="w">        </span><span class="c1">// This way we do get something with the `'iter` lifetime.</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">slice</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">iter</span> <span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>::<span class="n">core</span>::<span class="n">mem</span>::<span class="n">replace</span><span class="p">(</span><span class="n">at_slice</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">[]);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="n">second</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">slice</span><span class="p">.</span><span class="n">split_at_mut</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="n">at_slice</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">second</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">first</span><span class="p">.</span><span class="n">get_mut</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<ul>
<li>(Note that this implementation will panic when reaching the end of the slice, since the length check at <code>get_mut()</code> happens too late: it needs to be done before the <code>.split_at_mut()</code>, but that issue is not <code>IterMut</code>-specific)</li>
</ul>



<a name="214820678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214820678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214820678">(Oct 28 2020 at 10:59)</a>:</h4>
<p>Very nice! Thank you! I will digest this and maybe come back with more questions :D</p>



<a name="214824178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214824178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214824178">(Oct 28 2020 at 11:37)</a>:</h4>
<p>Is there a "generic" way of doing this? Like I tried with a HashMap: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4e008b94c8834d0fdfffe806b2e02a0e">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4e008b94c8834d0fdfffe806b2e02a0e</a> but I don't know what trick to use in this case</p>



<a name="214825914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214825914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214825914">(Oct 28 2020 at 11:56)</a>:</h4>
<p><a href="https://stackoverflow.com/q/25730586/155423">https://stackoverflow.com/q/25730586/155423</a></p>



<a name="214825956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214825956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214825956">(Oct 28 2020 at 11:57)</a>:</h4>
<p>Use the collections mutable iterator instead.</p>



<a name="214826395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214826395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214826395">(Oct 28 2020 at 12:02)</a>:</h4>
<p>I am designing my own collection :/ and also I would like to learn what my option for safe rust are.</p>



<a name="214826828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214826828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214826828">(Oct 28 2020 at 12:07)</a>:</h4>
<p>Bascally I have encoded a linked list in a vector, each element in the vector is a {T, prev,next} and I want to iterate in order of the elements and not in order of the slice/vec itself</p>



<a name="214827219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214827219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214827219">(Oct 28 2020 at 12:11)</a>:</h4>
<p>For now I am using a raw pointer + a PantomData&lt;&amp;mut&gt;</p>



<a name="214834336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834336">(Oct 28 2020 at 13:20)</a>:</h4>
<blockquote>
<p>I tried with a HashMap</p>
</blockquote>
<p>Which is why I said to use <code>HashMap</code>'s iterator <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="214834378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834378">(Oct 28 2020 at 13:21)</a>:</h4>
<p>In pure safe Rust, there's not much of an option</p>



<a name="214834447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834447">(Oct 28 2020 at 13:21)</a>:</h4>
<p>You have to look at what functions are provided by the underlying thing. In the code above, there's <code>split_at_mut</code></p>



<a name="214834537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834537">(Oct 28 2020 at 13:22)</a>:</h4>
<p>The larger ideas from above are explored in <a href="https://stackoverflow.com/q/34384089/155423">https://stackoverflow.com/q/34384089/155423</a></p>



<a name="214834613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834613">(Oct 28 2020 at 13:22)</a>:</h4>
<blockquote>
<p>each element in the vector is a {T, prev,next}</p>
</blockquote>
<p>What are the types of <code>prev</code> and <code>next</code>? That's rather important.</p>



<a name="214834690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214834690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214834690">(Oct 28 2020 at 13:23)</a>:</h4>
<p>If they are indices in the vector, it should be straight forward</p>



<a name="214835864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214835864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214835864">(Oct 28 2020 at 13:32)</a>:</h4>
<p>But it will still need unsafe because there's no guarantee that all of your <code>next</code> indices are distinct</p>



<a name="214835900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214835900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214835900">(Oct 28 2020 at 13:32)</a>:</h4>
<p>E.g. <code>{next: 1}, {next: 1}</code> would return the same mutable reference twice</p>



<a name="214835974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214835974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214835974">(Oct 28 2020 at 13:33)</a>:</h4>
<p>Which would violate the rules of references</p>



<a name="214841600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214841600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214841600">(Oct 28 2020 at 14:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code/near/214834690">said</a>:</p>
<blockquote>
<p>If they are indices in the vector, it should be straight forward</p>
</blockquote>
<p>They are indeed indexes <code>usize</code></p>



<a name="214842150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214842150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214842150">(Oct 28 2020 at 14:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code/near/214835900">said</a>:</p>
<blockquote>
<p>E.g. <code>{next: 1}, {next: 1}</code> would return the same mutable reference twice</p>
</blockquote>
<p>Why returning two times the same element twice would be bad according to borrow checking logic? I understand it's wrong from an iterator behavior point of view but you can call <code>slice.get_mut(index)</code> two times in a row without any problem, I believe. How is that different?</p>



<a name="214842358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214842358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214842358">(Oct 28 2020 at 14:18)</a>:</h4>
<p>Is it because one could do:</p>
<div class="codehilite"><pre><span></span><code>let a = iter.next().unwrap();
let b = iter.next().unwrap();
</code></pre></div>


<p>And potentially you get two times the same element?</p>



<a name="214842396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214842396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214842396">(Oct 28 2020 at 14:18)</a>:</h4>
<p>the two borrows can exist simultaneously iff the indexes are distinct</p>



<a name="214842410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214842410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214842410">(Oct 28 2020 at 14:18)</a>:</h4>
<p>and that would indeed break the rules! Thank you</p>



<a name="214842577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214842577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214842577">(Oct 28 2020 at 14:20)</a>:</h4>
<p>but rust can't reason about that so it makes the conservative assumption that they are not necessarily distinct and disallows concurrent borrows altogether, and you need a primitive like <code>split_at_mut</code> in order to express this in the type system</p>



<a name="214847431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214847431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214847431">(Oct 28 2020 at 14:55)</a>:</h4>
<blockquote>
<p>And potentially you get two times the same element?</p>
</blockquote>
<p>Yes. Or, easier, <code>iter.collect::&lt;Vec&lt;&gt;&gt;()</code></p>



<a name="214848104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214848104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214848104">(Oct 28 2020 at 15:00)</a>:</h4>
<p>All of this make perfect sense! Thank you very much!<br>
I guess in order to not have to use unsafe, I would need to "re-order" the element in my vec and use the standard slice itermut implementation</p>



<a name="214848600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214848600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214848600">(Oct 28 2020 at 15:03)</a>:</h4>
<p>Which, under the hood, uses unsafe because it knows its own invariants</p>



<a name="214850361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214850361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214850361">(Oct 28 2020 at 15:14)</a>:</h4>
<p>Yes of course, but I would always trust more the usage of unsafe code in the standard library rather than my own. Would miri help detect such bugs like reference aliasing etc?</p>



<a name="214869662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214869662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214869662">(Oct 28 2020 at 17:21)</a>:</h4>
<p>That should be one of the big things, yes.</p>



<a name="214869695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214869695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214869695">(Oct 28 2020 at 17:21)</a>:</h4>
<p>But you have to actually execute code that violates it</p>



<a name="214869769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214869769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214869769">(Oct 28 2020 at 17:22)</a>:</h4>
<p>So, if you just looped over the collection and used the reference then threw it away, it wouldn't complain</p>



<a name="214869811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214869811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214869811">(Oct 28 2020 at 17:22)</a>:</h4>
<p>But if you <code>collect</code>ed, then it'd error.</p>



<a name="214872316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214872316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214872316">(Oct 28 2020 at 17:39)</a>:</h4>
<p>Interesting! Thanks i'll improve my tests then :)</p>



<a name="214873397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/214873397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#214873397">(Oct 28 2020 at 17:47)</a>:</h4>
<p>(You might also have to <em>use</em> two mutable reference after collecting them, for MIRI to check the stack borrow annotations on them.  I don't remember for sure.)</p>



<a name="215241695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/215241695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ambiso <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#215241695">(Nov 01 2020 at 12:13)</a>:</h4>
<p><span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span> could you explain what you mean by "reborrowing"?</p>



<a name="215252881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/215252881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#215252881">(Nov 01 2020 at 17:10)</a>:</h4>
<p>The idea is that when you have nested reference types (in a quite broad sense, I am not talking about <code>&amp;</code> and<code>&amp;mut</code> exclusively), there are some situations where you can unflatten those: if somebody already had some borrow during some <code>'lifetime</code>, then you can borrow the borrow itself during a <code>'shorter</code> lifetime:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">example</span><span class="o">&lt;'</span><span class="na">lifetime</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">someone</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">lifetime</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Pointee</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">nested_borrow</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">mut</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">lifetime</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">Pointee</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">someone</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Then, the interesting part is, that for the duration of your <code>'shorter</code> (sub)-borrow, you can access the pointee.<br>
And practically speaking, the current double indirection of <code>&amp;mut &amp;mut</code> is an inefficient way to do so. So you can <strong>flatten the indirection</strong>, and obtain a direct borrow of the <code>Pointee</code>, <em>as long as it keeps the limitations the nested borrow would have had</em> (<em>e.g.</em>, lifetime of the outer borrow, <code>mut</code> if an only if all the borrow chain is <code>mut</code>):</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code>  fn example&lt;'lifetime&gt;(mut someone: &amp;'lifetime mut Pointee) -&gt; ...
  {
      let nested_borrow: &amp;'_ mut &amp;'lifetime mut Pointee = &amp;mut someone;
<span class="gi">+     let reborrow: &amp;'_ mut Pointee = &amp;mut** nested_borrow;</span>
  }
</code></pre></div>
<p>So, in summary, reborrowing is a mechanism to keep indirections flat / simple (instead of nested), but with the "tied to the sorter (outer) lifetime" nature than the natural nested borrow would have had.<br>
In practice, it is a way to avoid <em>moving</em> (and thus consuming and rendering unusable) a reference type that is not <code>Copy</code>, such as <code>&amp;'lifetime mut Pointee</code> in the example, by having implicitly <em>borrowed</em> it during the reborrow:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">example</span><span class="o">&lt;'</span><span class="na">lifetime</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">someone</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">lifetime</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Pointee</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_reborrow</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Pointee</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cm">/* &amp;mut* */</span><span class="w"> </span><span class="n">someone</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">stuff</span><span class="p">(</span><span class="n">someone</span><span class="p">);</span><span class="w"> </span><span class="c1">// OK.</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=75c0aea4f4c78b967e09e3ec75a052f7">Playground</a></li>
</ul>
<hr>
<p>In the case of <code>IterMut</code>, we had a case of nested borrows <code>fn next&lt;'next&gt;(&amp;'next mut &amp;'iter mut [u8])</code> and thus a reborrow being the natural / spontaneous way to flatten such indirection. But we thus get an unflattend borrow that yet carries the lifetime of the outer borrow, <code>'next</code>, instead of the needed / desired <code>'iter</code> one.</p>



<a name="215337917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/215337917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ambiso <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#215337917">(Nov 02 2020 at 15:59)</a>:</h4>
<p>Thanks a lot for taking the time (out of your lifetime <em>ba dum ts</em>)</p>



<a name="215571529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%20over%20mutable%20container%20without%20unsafe%20code/near/215571529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.20over.20mutable.20container.20without.20unsafe.20code.html#215571529">(Nov 04 2020 at 12:04)</a>:</h4>
<p><a href="https://github.com/Soveu/safer-std/blob/master/src/slice_iter_mut.rs">i have done this before :)</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>